#include "../inc/main.h"

// 获取触摸坐标=========================================================================================
int get_touch_pos(int *x, int *y)
{
    int temx = -1, temy = -1;
    int touch_fd = open("/dev/input/event0", O_RDWR);
    if (touch_fd < 0)
    {
        printf("open touch failed\n");
        return -1;
    }
    // printf("open touch success\n");

    struct input_event touch_event;
    while (1)
    {
        int read_ret = read(touch_fd, &touch_event, sizeof(struct input_event));
        if (read_ret < 0)
        {
            perror("read touch failed\n");
            close(touch_fd);
            return -1;
        }

        if (touch_event.type == EV_ABS)
        {
            if (touch_event.code == ABS_X)
            {
                temx = touch_event.value;
            }
            else if (touch_event.code == ABS_Y)
            {
                temy = touch_event.value;
            }
        }

        if (touch_event.type == EV_KEY && touch_event.code == BTN_TOUCH && touch_event.value == 1)
        {
            *x = temx;
            *y = temy;
            // printf("触摸坐标: %d %d\n", *x, *y);
            break;
        }
    }

    close(touch_fd);
    return 0;
}
